//
//  ChartCellFrame.m
//  气泡
//
//  Created by zzy on 14-5-13.
//  Copyright (c) 2014年 zzy. All rights reserved.
//
#define kIconMarginX 5
#define kIconMarginY 5

#import "ChartCellFrame.h"

@implementation ChartCellFrame

-(void)setChartMessage:(ChartMessage *)chartMessage
{
    _chartMessage=chartMessage;
    
//    CGSize winSize=[UIScreen mainScreen].bounds.size;
//    CGFloat iconX=kIconMarginX;
//    CGFloat iconY=kIconMarginY;
//    CGFloat iconWidth=40;
//    CGFloat iconHeight=40;
//    
//    if(chartMessage.messageType==kMessageFrom){
//      
//    }else if (chartMessage.messageType==kMessageTo){
//        iconX=winSize.width-kIconMarginX-iconWidth;
//    }
//    self.iconRect=CGRectMake(iconX, iconY, iconWidth, iconHeight);
//    
//    CGFloat contentX=CGRectGetMaxX(self.iconRect)+kIconMarginX;
//    CGFloat contentY=iconY;
//    NSDictionary *attributes = @{NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:13]};
//    CGSize contentSize=[chartMessage.content boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
//    if(chartMessage.messageType==kMessageTo){
//    
//        contentX=iconX-kIconMarginX-contentSize.width-iconWidth;
//    }
//    
//    self.chartViewRect=CGRectMake(contentX, contentY, contentSize.width+35, contentSize.height+30);
//    
//    self.cellHeight=MAX(CGRectGetMaxY(self.iconRect), CGRectGetMaxY(self.chartViewRect))+kIconMarginX;
    
    
    
    // 0、获取屏幕宽度
    CGFloat screenW = [UIScreen mainScreen].bounds.size.width;
    // 1、计算时间的位置
    if (_showTime){
        CGFloat timeY = kMargin;
        NSDictionary *timeAttributes = @{NSFontAttributeName: kTimeFont};
        CGSize timeSize =[_chartMessage.time boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:timeAttributes context:nil].size;
        CGFloat timeX = (screenW - timeSize.width) / 2;
        _timeF = CGRectMake(timeX, timeY, timeSize.width + kTimeMarginW, timeSize.height + kTimeMarginH);
    }
    // 2、计算头像位置
    CGFloat iconX = kMargin;
    // 2.1 如果是自己发得，头像在右边
    if (_chartMessage.messageType == kMessageTo) {
        iconX = screenW - kMargin - kIconWH;
    }
    
    CGFloat iconY = CGRectGetMaxY(_timeF) + kMargin;
    _iconRect = CGRectMake(iconX, iconY, kIconWH, kIconWH);
    
    // 3、计算内容位置
    CGFloat contentX = CGRectGetMaxX(_iconRect) + kMargin;
    CGFloat contentY = iconY;
    NSDictionary *contentAttributes = @{NSFontAttributeName: kContentFont};
    CGSize contentSize =[_chartMessage.content boundingRectWithSize:CGSizeMake(kContentW, CGFLOAT_MAX) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:contentAttributes context:nil].size;
    
    if (_chartMessage.messageType == kMessageTo) {
        contentX = iconX - kMargin - contentSize.width - kContentLeft - kContentRight;
    }
    
    _chartViewRect = CGRectMake(contentX, contentY, contentSize.width + kContentLeft + kContentRight, contentSize.height + kContentTop + kContentBottom);
    
    // 4、计算高度
    _cellHeight = MAX(CGRectGetMaxY(_chartViewRect), CGRectGetMaxY(_iconRect))  + kMargin;
    
}
@end

